package com.tech.arno.spicy.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tech.arno.spicy.JPush.PushMessageActivity;
import com.tech.arno.spicy.JPush.SpicyJPushReceiver;
import com.tech.arno.spicy.R;
import com.tech.arno.spicy.manager.DialogManager;
import com.tech.arno.spicy.module.PushMessage;
import com.tech.arno.spicy.mvp.presenter.LoginPresenter;
import com.tech.arno.spicy.mvp.presenterImpl.LoginPresenterImpl;
import com.tech.arno.spicy.mvp.view.LoginView;
import com.tech.arno.spicy.ui.view.associatemail.MailBoxAssociateTokenizer;
import com.tech.arno.spicy.ui.view.associatemail.MailBoxAssociateView;


public class LoginActivity extends BaseActivity implements LoginView, View.OnClickListener {

    //自定义登陆广播Action
    public static final String LOGIN_ACTION = "com.spicy.LOGIN_ACTION";
    /**
     * UI
     */
    private MailBoxAssociateView mUserNameAssociateView;
    private EditText mPasswordView;
    private TextView mLoginView;
    private LoginPresenter presenter;

    /**
     * data
     */
    private PushMessage mPushMessage; // 推送过来的消息
    private boolean fromPush; // 是否从推送到此页面

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_layout);
        initData();
        initView();
    }

    @Override
    protected void onDestroy() {
        presenter.onDestroy();
        super.onDestroy();
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent.hasExtra(SpicyJPushReceiver.PUSH_MESSAGE)) {
            //如果有推送消息则获取信息
            mPushMessage = (PushMessage) intent.getSerializableExtra(SpicyJPushReceiver.PUSH_MESSAGE);
        }
        fromPush = intent.getBooleanExtra(SpicyJPushReceiver.FROM_PUSH, false);
    }

    private void initView() {

        mUserNameAssociateView = (MailBoxAssociateView) findViewById(R.id.associate_email_input);
        mPasswordView = (EditText) findViewById(R.id.login_input_password);
        mLoginView = (TextView) findViewById(R.id.login_button);
        mLoginView.setOnClickListener(this);

        mUserNameAssociateView = (MailBoxAssociateView) findViewById(R.id.associate_email_input);
        String[] recommendMailBox = getResources().getStringArray(R.array.recommend_mailbox);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.item_associate_mail_list,
                R.id.tv_recommend_mail, recommendMailBox);
        mUserNameAssociateView.setAdapter(adapter);
        mUserNameAssociateView.setTokenizer(new MailBoxAssociateTokenizer());

        presenter = new LoginPresenterImpl(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.login_button:
                String userName = mUserNameAssociateView.getText().toString().trim();
                String password = mPasswordView.getText().toString().trim();

                if (TextUtils.isEmpty(userName)) {
                    Toast.makeText(this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                DialogManager.getInstnce().showProgressDialog(this);
                //登录操作
                presenter.login(userName, password);
                break;
        }
    }


    //登录成功回调
    @Override
    public void loginSuccess() {
        connectToSever();
        //拿到登录信息 发送登录成功局部广播
        sendLoginBroadcast();
        //登录成功并且从推送过来 跳转到推送展示页面
        if (fromPush) {
            Intent intent = new Intent(LoginActivity.this, PushMessageActivity.class);
            //启动模式为NEW_TASK
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(SpicyJPushReceiver.PUSH_MESSAGE, mPushMessage);
            startActivity(intent);
        }
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        // 隐藏软键盘
        assert imm != null;
        imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
        finish();//销毁当前登陆页面
    }

    //登录错误回调
    @Override
    public void loginError() {
        DialogManager.getInstnce().dismissProgressDialog();
        Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
    }

    //向整个应用发送登陆广播事件
    @Override
    public void sendLoginBroadcast() {
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(LOGIN_ACTION));
    }

    //启动长连接
    @Override
    public void connectToSever() {
//        startService(new Intent(LoginActivity.this, MinaService.class));
    }

}
